package von.seiji.cn.imageT;


import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.URLUtil;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class 阈值 {

    static {

        URL url = ClassLoader.getSystemResource("data/opencv_java454.dll");
        System.load(url.getPath());
    }

    public static void main(String[] args) throws Exception {
        String url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202008%2F29%2F20200829130145_anbvf.thumb.400_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644050338&t=a7da518629fb683155578685e80a9f2d";

        byte[] bytes = IoUtil.readBytes(URLUtil.getStream(new URL(url)));
        Mat source = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_COLOR);
        Imgproc.cvtColor(source, source, Imgproc.COLOR_RGB2GRAY);

//        HighGui.imshow("L0", source);
//        Mat mat0 = new Mat();
//        Imgproc.resize(source, mat0, new Size(source.width()/2, source.height()/2));
//        HighGui.imshow("size", mat0);
        Mat mat = new Mat();
        Imgproc.threshold(source, mat, 127, 255, Imgproc.THRESH_BINARY);
        Mat mat1 = new Mat();
        Imgproc.threshold(source, mat1, 127, 255, Imgproc.THRESH_BINARY_INV);
        Mat mat2 = new Mat();
        Imgproc.threshold(source, mat2, 127, 255, Imgproc.THRESH_TRUNC);
        Mat mat3 = new Mat();
        Imgproc.threshold(source, mat3, 127, 255, Imgproc.THRESH_TOZERO);
        Mat mat4 = new Mat();
        Imgproc.threshold(source, mat4, 127, 255, Imgproc.THRESH_TOZERO_INV);
        Mat mat5 = new Mat();
//        Imgproc.cvtColor(source, source, Imgproc.COLOR_RGB2GRAY);
        Imgproc.threshold(source, mat5, 127, 255, Imgproc.THRESH_OTSU);
        ArrayList<String> strings = Lists.newArrayList("original Image", "binary", "binary_inv"
                , "trunc", "tozero", "tozero_inv", "Otsu");
        ArrayList<Mat> mats = Lists.newArrayList(source, mat, mat1, mat2, mat3, mat4, mat5);
        for (int i = 0; i < mats.size(); i++) {
//            Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\temp\\pic\\" + strings.get(i) + ".jpg", mats.get(i));
            HighGui.imshow(strings.get(i), mats.get(i));
        }
        HighGui.waitKey(0);
    }

    @Test
    public void dslfk() {
        Mat origin = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\temp\\pic\\haha.jpg");

        Imgproc.cvtColor(origin, origin, Imgproc.COLOR_RGB2GRAY);
        HighGui.imshow("L", origin);
        Mat clone = origin.clone();
//        Imgproc.GaussianBlur(clone, clone, new Size(3, 3), 1, 1);

        Imgproc.threshold(clone, clone, 0, 255, Imgproc.THRESH_BINARY_INV);
        HighGui.imshow("L1", clone);
        Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\temp\\pic\\L.jpg", clone);

        HighGui.waitKey(0);
    }

    @Test
    public void dkslf() throws MalformedURLException {
        String url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202008%2F29%2F20200829130145_anbvf.thumb.400_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644050338&t=a7da518629fb683155578685e80a9f2d";

        byte[] bytes = IoUtil.readBytes(URLUtil.getStream(new URL(url)));
        Mat source = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_COLOR);
        Imgproc.cvtColor(source, source, Imgproc.COLOR_RGB2GRAY);
//        Mat mat2 = new Mat();
        Imgproc.medianBlur(source, source, Imgproc.COLOR_BGRA2RGBA);
        Mat mat3 = new Mat();
        Imgproc.threshold(source, mat3, 127, 255, Imgproc.THRESH_BINARY);

        Mat mat = new Mat();
        Imgproc.adaptiveThreshold(source, mat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C
                , Imgproc.THRESH_BINARY, 11, 2);
        Mat mat1 = new Mat();
        Imgproc.adaptiveThreshold(source, mat1, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C
                , Imgproc.THRESH_BINARY, 11, 2);

        ArrayList<String> strings = Lists.newArrayList("original image", "global thresholding"
                , "adaptive mean thresholding", "adaptive gaussian thresholding");
        ArrayList<Mat> mats = Lists.newArrayList(source, mat3, mat, mat1);
        for (int i = 0; i < mats.size(); i++) {
            Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\temp\\pic\\" + strings.get(i) + ".jpg", mats.get(i));
            HighGui.imshow(strings.get(i), mats.get(i));
        }
        HighGui.waitKey(0);
    }
}
